/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.action;

import cassys.dataaccess.DAOException;
import cassys.datatransferobjects.AssessmentDTO;
import cassys.datatransferobjects.AssessmentItemsDTO;
import cassys.datatransferobjects.EvaluationDTO;
import cassys.datatransferobjects.EvaluationItemDTO;
import cassys.datatransferobjects.ModuleDTO;
import cassys.datatransferobjects.StudentDTO;
import cassys.delegates.AssessmentDelegate;
import cassys.delegates.EvaluationDelegate;
import cassys.delegates.ModuleDelegate;
import cassys.delegates.StudentModuleTeamDelegate;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Priyanka
 */
public class EvaluationAction extends CassysSupport {

    private ArrayList<String> ids;
    private ArrayList<Integer> marks;
    private String modulecode;
    private String selectedStudent;
    private String selectedAssess;
    private String selectedAssessItem;
    private String evalItemId;
    private AssessmentDelegate delegate;
    private EvaluationDelegate evaldelegate;
    private ModuleDelegate mDelegate;
    private StudentModuleTeamDelegate stdModDelegate;
    private ArrayList<AssessmentDTO> assessments;
    private ArrayList<StudentDTO> studentlist;
    private AssessmentDTO assessment;
    private AssessmentItemsDTO assessmentItem;
    private EvaluationItemDTO evaluationItem;
    private boolean isUpdate = false;
    Random rand = new Random();
    private ArrayList<AssessmentDTO> assessmentsResult;

    
    public void setAssessmentsResult(ArrayList<AssessmentDTO> assessmentsResult) {
        this.assessmentsResult = assessmentsResult;
    }

    public ArrayList<AssessmentDTO> getAssessmentsResult() throws DAOException {
        
        
        assessmentsResult = new ArrayList<AssessmentDTO>();
        assessmentsResult = delegate.getAllAssessmentByModule(modulecode);
        
        System.out.print("555"+assessmentsResult.get(0).getAssessmentItem(0).getAssessmentItemId());
        
        if (assessmentsResult != null && assessmentsResult.size() > 0) {
            setSelectedAssess(assessmentsResult.get(0).getAssessmentId());
        }
        if (selectedAssess != null && selectedAssess.length() > 0) {
            setSelectedAssessItem(assessmentsResult.get(0).getAssessmentItems().get(0).getAssessmentItemId());
        }
        isUpdate = false;
        return assessmentsResult;
    }

    @Override
    public String execute() throws Exception {
        return "addevaluation";
    }

    public EvaluationAction() {
        delegate = new AssessmentDelegate();
        evaldelegate = new EvaluationDelegate();
        mDelegate = new ModuleDelegate();
        stdModDelegate = new StudentModuleTeamDelegate();
    }

    public List<String> getStudents() throws Exception {
        studentlist = stdModDelegate.getAllStudentsInModule(modulecode);
        ArrayList<String> students = new ArrayList<String>();
        Iterator<StudentDTO> iter = studentlist.iterator();
        while (iter.hasNext()) {
            StudentDTO dto = iter.next();
            students.add(dto.getUserId());
        }
        return students;

    }

    public AssessmentDTO getAssessment() {
        assessment = null;
        try {
            System.out.println("selected" + selectedAssess);
            assessment = delegate.findAssessment(selectedAssess);
            System.out.println("selected" + assessment.getAssessmentId());
            System.out.println("selected" + assessment.getTitle());
            System.out.println("selected" + assessment.getOwner());
        } catch (DAOException ex) {
            Logger.getLogger(EvaluationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return assessment;
    }

    public AssessmentItemsDTO getAssessmentItem() {
        assessmentItem = null;
        if (assessment != null && assessment.getAssessmentItems().size() > 0) {
            Iterator<AssessmentItemsDTO> iter = assessment.getAssessmentItems().iterator();
            while (iter.hasNext()) {
                AssessmentItemsDTO temp = iter.next();
                if (temp.getAssessmentItemId().equalsIgnoreCase(selectedAssessItem)) {
                    assessmentItem = temp;
                }
            }
        }
        return assessmentItem;
    }

    public String list_Students() throws Exception {
        assessments = new ArrayList<AssessmentDTO>();
        assessments = delegate.getAllAssessmentByModule(modulecode);
        if (assessments != null && assessments.size() > 0) {
            setSelectedAssess(assessments.get(0).getAssessmentId());
        }
        if (selectedAssess != null && selectedAssess.length() > 0) {
            setSelectedAssessItem(assessments.get(0).getAssessmentItems().get(0).getAssessmentItemId());
        }
        isUpdate = false;
        return "successIndividual";
    }

    public String list_Teams() {
        try {
            System.out.println("am here in teams" + modulecode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "successTeam";
    }

    public ArrayList<StudentDTO> getStudentlist() {
        return studentlist;
    }

    public void setStudentlist(ArrayList<StudentDTO> studentlist) {
        this.studentlist = studentlist;
    }

    public List<ModuleDTO> getModules() throws Exception {
        ArrayList<ModuleDTO> modules = mDelegate.findAllModules();
        return modules;
    }

    public String getSelectedStudent() {
        return selectedStudent;
    }

    public void setSelectedStudent(String selectedStudent) {
        this.selectedStudent = selectedStudent;
    }

    public String getModulecode() {
        return modulecode;
    }

    public void setModulecode(String modulecode) {
        this.modulecode = modulecode;
    }

    public String getSelectedAssess() {
        return selectedAssess;
    }

    public void setSelectedAssess(String selectedAssess) {
        this.selectedAssess = selectedAssess;
    }

    public List<AssessmentDTO> getAssessments() {
        return assessments;
    }

    public String getSelectedAssessItem() {
        return selectedAssessItem;
    }

    public void setSelectedAssessItem(String selectedAssessItem) {
        this.selectedAssessItem = selectedAssessItem;
    }

    public EvaluationItemDTO getEvaluationItem() {
        return evaluationItem;
    }

    public void setEvaluationItem(EvaluationItemDTO evaluationItem) {
        this.evaluationItem = evaluationItem;
    }

    public String individualEvaluation() {
        System.out.println("modulecode" + modulecode);
        System.out.println("selectedStudent" + selectedStudent);

        int i = 0;
        for (String id : ids) {
            try {
                StringTokenizer tokenizer = new StringTokenizer(id, "_");
                String selectedAssess = tokenizer.nextToken();
                String selectedAssessItem = tokenizer.nextToken();
                int mark = marks.get(i);
                i++;

                EvaluationDTO evaluation = null;

                /*if (evaldelegate.findEvalByAssess(selectedAssess) != null) {
                    evaluation = evaldelegate.findEvalByAssess(selectedAssess);
                    for (EvaluationItemDTO item : evaluation.getEvalutionItems()) {
                        System.out.println("TEST"+item.getAssessmentItem());
                        if (item.getAssessmentItem().getAssessmentId().equals(selectedAssessItem)) {
                            item.setMark(mark);
                        }
                    }
                } else {*/

                    evaluation = new EvaluationDTO();

                    List<EvaluationItemDTO> evalutionItems = new ArrayList<EvaluationItemDTO>();

                    evaluation.setAssessmentId(selectedAssess);
                    evaluation.setStudentId(getSelectedStudent());
                    evaluation.setEvaluationId("E-" + rand.nextInt());

                    EvaluationItemDTO item = new EvaluationItemDTO();
                    AssessmentItemsDTO assessmentItem = new AssessmentItemsDTO();
                    assessmentItem.setAssessmentId(selectedAssessItem);
                    item.setAssessmentItem(assessmentItem);
                    item.setEvalutionItemId("EI-" + rand.nextInt());
                    item.setEvaluationId(evaluation.getEvaluationId());
                    item.setMark(mark);
                    setEvalItemId(item.getEvalutionItemId());
                    evalutionItems.add(item);

                    evaluation.setEvalutionItems(evalutionItems);
                //}
                evaldelegate.insertIndividualEvaluation(evaluation);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return "listexisting";
    }

    public String teamEvaluation() {
        try {
            System.out.println("am here in teams===>" + modulecode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "sucessTeamEvaluation";
    }

    public String getEvalItemId() {
        return evalItemId;
    }

    public void setEvalItemId(String evalItemId) {
        this.evalItemId = evalItemId;
    }

    public boolean isIsUpdate() {
        return isUpdate;
    }

    public void setIsUpdate(boolean isUpdate) {
        this.isUpdate = isUpdate;
    }

    public ArrayList<String> getIds() {
        return ids;
    }

    public void setIds(ArrayList<String> ids) {
        this.ids = ids;
    }

    public ArrayList<Integer> getMarks() {
        return marks;
    }

    public void setMarks(ArrayList<Integer> marks) {
        this.marks = marks;
    }
}
